package com.chipsit.result;

import com.chipsit.constant.Constants;
import com.chipsit.constant.Message;
import lombok.Data;

import java.io.Serial;
import java.io.Serializable;

/**
 * 统一返回结果类
 */
@Data
public class R<T> implements Serializable {
    @Serial
    private static final long serialVersionUID = 1L;

    /**
     * 成功
     */
    public static final int SUCCESS = Constants.SUCCESS;

    /**
     * 失败
     */
    public static final int ERROR = Constants.ERROR;

    private int code;

    private String msg;

    private T data;

    public static <T> R<T> success() {
        return response(SUCCESS, Message.SUCCESS.getMessage(), null);
    }

    public static <T> R<T> success(String msg) {
        return response(SUCCESS, msg, null);
    }

    public static <T> R<T> success(T data) {
        return response(SUCCESS, Message.SUCCESS.getMessage(), data);
    }

    public static <T> R<T> success(String msg, T data) {
        return response(SUCCESS, msg, data);
    }

    public static <T> R<T> error() {
        return response(ERROR, Message.ERROR.getMessage(), null);
    }

    public static <T> R<T> error(String msg) {
        return response(ERROR, msg, null);
    }

    public static <T> R<T> error(String msg, T data) {
        return response(ERROR, msg, data);
    }

    public static <T> R<T> fail(int code, String msg) {
        return response(code, msg, null);
    }

    private static <T> R<T> response(int code, String msg, T data) {
        R<T> response = new R<>();
        response.setCode(code);
        response.setMsg(msg);
        response.setData(data);
        return response;
    }
}
